package com.dapeng.cleardatabackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dapeng.cleardatabackend.common.BaseResponse;
import com.dapeng.cleardatabackend.common.ResultUtils;
import com.dapeng.cleardatabackend.model.dto.relation.RelationAddRequest;
import com.dapeng.cleardatabackend.model.dto.relation.RelationDeleteRequest;
import com.dapeng.cleardatabackend.model.dto.relation.RelationGetRequest;
import com.dapeng.cleardatabackend.model.dto.relation.RelationUpdateRequest;
import com.dapeng.cleardatabackend.model.vo.NodeVO;
import com.dapeng.cleardatabackend.model.vo.RelationVO;
import com.dapeng.cleardatabackend.service.RelationService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author dapeng
 * @description 图谱关系控制器
 * @date 2024/12/22 22:42
 */
@RestController
@RequestMapping("/relation")
public class RelationController {

    @Resource
    private RelationService relationService;

    /**
     * 获取关系列表
     *
     * @param relationGetRequest 获取节点请求
     * @return {@link BaseResponse }<{@link Page }<{@link NodeVO }>>
     */
    @PostMapping("/page")
    public BaseResponse<Page<RelationVO>> getRelationPage(@RequestBody @Valid RelationGetRequest relationGetRequest) {
        Page<RelationVO> page = relationService.getRelationPage(relationGetRequest);
        return ResultUtils.success(page);
    }

    /**
     * 获取原始关系列表
     *
     * @param relationGetRequest 获取节点请求
     * @return {@link BaseResponse }<{@link Page }<{@link NodeVO }>>
     */
    @PostMapping("/page/original")
    public BaseResponse<Page<RelationVO>> getOriginalRelationPage(@RequestBody @Valid RelationGetRequest relationGetRequest) {
        Page<RelationVO> page = relationService.getOriginalRelationPage(relationGetRequest);
        return ResultUtils.success(page);
    }


    /**
     * 创建关系
     *
     * @param relationAddRequest 关系更新请求
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/add")
    public BaseResponse<Long> addRelation(@RequestBody @Valid RelationAddRequest relationAddRequest) {
        return ResultUtils.success(relationService.addSingleRelation(relationAddRequest));
    }

    /**
     * 更新关系
     *
     * @param relationUpdateRequest 关系更新请求
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateRelation(@RequestBody @Valid RelationUpdateRequest relationUpdateRequest) {
        return ResultUtils.success(relationService.updateRelation(relationUpdateRequest));
    }

    /**
     * 删除关系
     *
     * @param relationDeleteRequest 关系删除请求
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteRelation(@RequestBody @Valid RelationDeleteRequest relationDeleteRequest) {
        return ResultUtils.success(relationService.deleteRelation(relationDeleteRequest));
    }

    /**
     * 下载
     *
     * @param id id
     * @return {@link ResponseEntity }<{@link byte[] }>
     */
    @GetMapping("/download/{id}")
    public ResponseEntity<byte[]> download(@PathVariable("id") Long id) {
        return relationService.downloadCsv(id);
    }

    /**
     * 下载原始关系文件
     *
     * @param id id
     */
    @GetMapping("/download/original/{id}")
    public ResponseEntity<byte[]> downloadOriginal(@PathVariable("id") Long id) {
        return relationService.downloadOriginalCsv(id);
    }
}
